package com.car.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.car.common.Result;
import com.car.entity.MaintenanceRecord;
import com.car.service.MaintenanceRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 保养记录控制器
 *
 * @author Car System
 */
@Tag(name = "保养记录管理")
@RestController
@RequestMapping("/maintenance")
@RequiredArgsConstructor
public class MaintenanceRecordController {

    private final MaintenanceRecordService maintenanceRecordService;

    @Operation(summary = "分页查询保养记录")
    @GetMapping("/page")
    public Result<IPage<MaintenanceRecord>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        IPage<MaintenanceRecord> page = maintenanceRecordService.page(current, size, keyword, startTime, endTime);
        return Result.success(page);
    }

    @Operation(summary = "根据车辆ID查询保养记录")
    @GetMapping("/vehicle/{vehicleId}")
    public Result<List<MaintenanceRecord>> listByVehicleId(@PathVariable Long vehicleId) {
        List<MaintenanceRecord> list = maintenanceRecordService.listByVehicleId(vehicleId);
        return Result.success(list);
    }

    @Operation(summary = "根据客户ID查询保养记录")
    @GetMapping("/customer/{customerId}")
    public Result<List<MaintenanceRecord>> listByCustomerId(@PathVariable Long customerId) {
        List<MaintenanceRecord> list = maintenanceRecordService.listByCustomerId(customerId);
        return Result.success(list);
    }

    @Operation(summary = "根据ID查询保养记录")
    @GetMapping("/{id}")
    public Result<MaintenanceRecord> getById(@PathVariable Long id) {
        MaintenanceRecord record = maintenanceRecordService.getById(id);
        return Result.success(record);
    }

    @Operation(summary = "新增保养记录")
    @PostMapping
    public Result<Void> save(@RequestBody MaintenanceRecord record) {
        maintenanceRecordService.save(record);
        return Result.success("新增成功");
    }

    @Operation(summary = "更新保养记录")
    @PutMapping
    public Result<Void> update(@RequestBody MaintenanceRecord record) {
        maintenanceRecordService.update(record);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除保养记录")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        maintenanceRecordService.delete(id);
        return Result.success("删除成功");
    }

    @Operation(summary = "导出保养记录Excel")
    @GetMapping("/export")
    public void export(
            HttpServletResponse response,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) throws IOException {
        
        List<MaintenanceRecord> list = maintenanceRecordService.exportList(keyword, startTime, endTime);
        
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("保养记录", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        
        EasyExcel.write(response.getOutputStream(), MaintenanceRecord.class).sheet("保养记录").doWrite(list);
    }
}

